<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\System;


use App\Http\Controllers\Controller;
use App\Models\About;
use App\Models\Admin;
use App\Models\Brand;
use App\Models\Good;
use App\Models\GoodsOneType;
use App\Models\GoodsTwoType;
use App\Models\IntegralGood;
use App\Models\Message;
use App\Models\MessageUser;
use App\Models\Order;
use App\Models\OrderDetail;
use App\Models\Supplier;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class AboutController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\System\AboutController";

    public function index(Request $request)
    {
        $about = About::select('t1.*', 't2.nickname as admin_name')
            ->from((new About())->getTable() . ' as t1')
            ->leftJoin((new Admin())->getTable() . ' as t2', 't1.admin_id', '=', 't2.id')
            ->first();
        if ($about) {
            if (isset($about['updatetime'])) {
                $about['updatetime'] = date("Y-m-d H:i:s", $about['updatetime']);
            }
        }
        return $this->apiResponse($about);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $data = $request->only('intro', 'phone','copyright1','copyright2');
        if (!isset($data['intro']) || !isset($data['phone']) || !isset($data['copyright2']) || !isset($data['copyright2'])) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $time = time();
        $data['updatetime'] = $time;
        $data['admin_id'] = $request->ADMINID;
        About::where('id','!=',0)->update($data);
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = "修改";
        $content = "修改了关于我们的内容为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "关于我们模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }
}